import adwin.mysqlDemo as adwinDb
import adwin2pro.proMysqlClient as proClient

proCartoons = [];

def exitCartoon(adwinCartoon):
    for proCartoon in proCartoons:
        if proCartoon[0] == adwinCartoon[0]:
            return True
    return False

# 增量更新
def incrementalUpdate(adwinCartoon, latestChapterNum):
    adwinChapters = adwinDb.selectChaptersByIdBiggerThenNum(adwinCartoon[0], latestChapterNum);
    for adwinChapter in adwinChapters:
        proClient.addNewChapter(adwinChapter)

def merge():
    global proCartoons

    if proCartoons == None or proCartoons.__len__() == 0:
        proCartoons += proClient.getAllCartoons();

    adwinCartoons = adwinDb.getAllCartoon();

    for adwinCartoon in adwinCartoons:
        latestChapterNum = 0;
        if not exitCartoon(adwinCartoon):#漫画在生产中不存在，添加新的漫画介绍首页
            proClient.addNewCartoon(adwinCartoon)
        else:# 已经存在，查询最后条记录
            result = proClient.getLastestChapterOfCartoon(adwinCartoon[0])
            if result != None:
                latestChapterNum = result[2]

        # 增量更新漫画
        incrementalUpdate(adwinCartoon, latestChapterNum);


merge();
